package com.yanfei.zero.infrastructure.es;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.stereotype.Service;

import com.alibaba.cola.statemachine.StateMachine;
import com.yanfei.zero.domain.user.model.User1;
import com.yanfei.zero.domain.user.model.es.EsUser1;
import com.yanfei.zero.domain.user.repository.User1Mapper;
import com.yanfei.zero.infrastructure.config.ColaStatemachineConfig.User1Event;
import com.yanfei.zero.infrastructure.config.ColaStatemachineConfig.User1State;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public  class OrderEsService{
	@Resource
	private ElasticsearchOperations   esTemplate ;
	
	@Resource  
    private User1Mapper user1Mapper;  
	
	@Resource
    private StateMachine<User1State, User1Event, User1> orderStateMachine;
	
	public void importData() {
		// 查出数据
        user1Mapper.streamQuery(user->{
            User1 u1 = user.getResultObject();
            EsUser1 euser = new EsUser1();
            BeanUtils.copyProperties(u1, euser);
            esTemplate.save(euser);
            System.out.println("===@@@=======");
        });
    }
	
    public Object performPagedSearch() {  
        Criteria criteria = new Criteria();
        // 如果需要添加排序规则
        Sort sort = Sort.by(Sort.Order.asc("_score"));
        PageRequest pageable = PageRequest.of(1, 100); 
        // 创建一个SearchQuery对象
        CriteriaQuery searchQuery = new CriteriaQuery(criteria)
                .setPageable(pageable)
                .addSort(sort);

        // 执行搜索并获取结果
        SearchHits<EsUser1> result = esTemplate.search(searchQuery, EsUser1.class, IndexCoordinates.of("user1"));
        return result.getSearchHits();
    }
    
    
    
    // 创建订单
    public User1 create() {
        return orderOperation(0, User1State.INIT, User1Event.CREATE_SUCCESS);
    }
   // 支付
    public User1 pay(int id) {
        return orderOperation(id, User1State.WAIT_PAYMENT, User1Event.PAY_SUCCESS);
    }

   // 发货
    public User1 deliver(int id) {
        return orderOperation(id, User1State.WAIT_DELIVER, User1Event.DELIVERED_SUCCESS);
    }
    // 收货
    public User1 receive(int id) {
        return orderOperation(id, User1State.WAIT_RECEIVE, User1Event.RECEIVED_SUCCESS);
    }

    public List<User1> getOrders() {
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("pageSize", 1);
        parameterMap.put("offset", 10); 
        return user1Mapper.selectAll(parameterMap);
    }

    private User1 orderOperation(int id, User1State orderState, User1Event orderStatusChangeEvent) {
        String machineId = orderStateMachine.getMachineId();
        log.info("订单状态机：{}", machineId);
        User1 order = new User1();
        if(id!=0) {
             order = user1Mapper.selectById(id);
        }
        order.setStatus(orderState.toString());
        // 触发状态机
        User1State orderState1 = orderStateMachine.fireEvent(orderState, orderStatusChangeEvent, order);
        log.info("订单状态:{}", orderState1);
        // 返回订单
        return order;
    }

}
